#include <string>

using namespace std;

class Solution {
public:
    string countAndSay(int n) {
        if (n == 1) return "1";
        string prev = countAndSay(n-1);
        string res = "";
        int count = 0;
        char curr;
        for (int i=0; i<=prev.size(); ++i) {
            if (i == 0) {
                curr = prev[0];
                count++;
            } else if (i == prev.size()) {
                res.push_back(count + '0');
                res.push_back(curr);
            } else if (prev[i-1] != prev[i]) {
                res.push_back(count + '0');
                res.push_back(curr);
                curr = prev[i];
                count = 1;
            } else {
                count++;
            }
        }
        return res;
    }
};
